package cat.joronya.supermanager.service;

import android.util.Log;
import cat.joronya.supermanager.Supermanager;

public class LoginParserProvider 
{
	private static Class parsers[] = {
			cat.joronya.supermanager.service.htmlcleanerImpl.LoginParserImpl.class, 
			cat.joronya.supermanager.service.substrImpl.LoginParserImpl.class
		};
	
	// return the first registered parser
	public static LoginParser getDefault() 
	{
		try {
			for (Class parser : parsers) 
			{
				//We are only expecting one
				Log.d(Supermanager.TAG, "Loaded login parser impl: "+parser.getCanonicalName());
				LoginParser lp;

				lp = (LoginParser)parser.newInstance();
				return lp;
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		throw new Error ("No LoginParser registered");
	}
	
	public static LoginParser getImpl(Package packageName)
	{
		try {
			for (Class parser : parsers) 
			{
				// check package name
				if( !parser.getPackage().equals(packageName) )
					continue;
				
				Log.d(Supermanager.TAG, "Loaded login parser impl: "+parser.getCanonicalName());
				LoginParser lp;

				lp = (LoginParser)parser.newInstance();
				return lp;
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		throw new Error ("No LoginParser registered");
	}
}
